#!/bin/bash

# 设置脚本在发生错误时停止执行
set -e

# 输出日志函数
log() {
    echo "$(date +'%Y-%m-%d %H:%M:%S') - INFO - $1"
}

# 错误处理函数
error() {
    echo "$(date +'%Y-%m-%d %H:%M:%S') - ERROR - $1"
    exit 1
}

# 验证 calico-system 命名空间中的 Pod 状态
log "Checking pods in calico-system namespace..."
kubectl get pod -n calico-system
if [ $? -ne 0 ]; then
    error "Failed to get pods in calico-system namespace."
fi
log "Pods in calico-system namespace are running."

# 验证集群可用性，查看节点状态
log "Checking Kubernetes node status..."
kubectl get node
if [ $? -ne 0 ]; then
    error "Failed to get Kubernetes node status."
fi
log "Kubernetes node status checked successfully."

# 查看集群健康情况
log "Checking Kubernetes cluster health..."
kubectl get cs
if [ $? -ne 0 ]; then
    error "Failed to get Kubernetes cluster health."
fi
log "Kubernetes cluster health checked successfully."

# 查看 kube-system 命名空间中的 Pod 运行情况
log "Checking pods in kube-system namespace..."
kubectl get pod -n kube-system
if [ $? -ne 0 ]; then
    error "Failed to get pods in kube-system namespace."
fi
log "Pods in kube-system namespace are running."

log "All tasks completed successfully."